<template>
  <div class="content">
    <div class="title">Register Form</div>

    <div class="form">
      <el-form
        ref="form"
        label-position="right"
        :rules="rules"
        :model="form"
        label-width="160px">
        <el-form-item label="Name" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>

        <el-form-item prop="email" label="Email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>

        <el-form-item label="Phone Number" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>

        <el-form-item label="Password" prop="pass">
          <el-input
            type="password"
            v-model="form.pass"
            show-password></el-input>
        </el-form-item>
        <el-form-item label="Repeat Password" prop="checkPass">
          <el-input
            type="password"
            v-model="form.checkPass"
            show-password></el-input>
        </el-form-item>

        <el-form-item label="Gender" prop="genderSelect">
          <el-select
            clearable
            v-model="form.genderSelect"
            placeholder="——select——">
            <el-option label="Female" value="Female"></el-option>
            <el-option label="Male" value="Male"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="Country" prop="countrySelect">
          <el-select
            clearable
            filterable
            v-model="form.countrySelect"
            placeholder="——select——">
            <el-option
              v-for="item in countryList"
              :key="item.value"
              :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="I work or study at">
          <el-input v-model="form.workOrstudy"></el-input>
        </el-form-item>

        <el-form-item label="I use SISU AA mainly for">
          <el-select
            clearable
            v-model="form.sisuSelect"
            placeholder="——select——">
            <el-option
              label="Study of linguistics, social science or humanities"
              value="Study of linguistics, social science or humanities"></el-option>
            <el-option
              label="Research of IT, AI or NLP"
              value="Research of IT, AI or NLP"></el-option>
            <el-option label="Other" value="Other"></el-option>
            <!-- <el-option label="None" value="None"></el-option> -->
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-checkbox-group class="check" v-model="form.agree">
            <el-checkbox name="type"></el-checkbox>
          </el-checkbox-group>
          <div class="check_article" :class="{ warm: !form.agree }" ref="warm">
            I've read and accept the
            <span @click="handleURL" :class="{ warm: !form.agree }"
              >Terms of Use and Data</span
            >
          </div>
        </el-form-item>

        <el-form-item>
          <el-button
            :disabled="!form.agree"
            class="btn"
            @click="onSubmit('form')"
            >Submit</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { country } from '@/utils/country'
import { signup } from '@/api/login.js'

import axios from 'axios'

export default {
  data() {
    let validatePass = (rule, value, callback) => {
      let regx = /^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}$/
      if (value === '') {
        callback(new Error('Please enter a password'))
      } else if (value.length < 6) {
        callback(new Error('Your password must be at least six characters'))
      } else if (!regx.test(value)) {
        callback(
          new Error(
            'Your password must contain letters and numbers'
          )
        )
      } else {
        if (this.form.checkPass !== '') {
          this.$refs.form.validateField('checkPass')
        }
        callback()
      }
    }
    let validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('Please enter your password again'))
      } else if (value !== this.form.pass) {
        callback(new Error('The password is entered twice inconsistently!'))
      } else {
        callback()
      }
    }

    let validEmail = (rule, value, callback) => {
      let reg =
        /^[A-Za-z0-9-_\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/

      let email = '237391c1498@qq.com'
      if (value === '') {
        callback(new Error('Please enter your email address!!'))
      }
      if (!reg.test(value)) {
        callback(new Error('Please enter a correct email address!!'))
      } else if (value === email) {
        //   发送网络请求，验证邮箱是否存在
        callback(new Error('The mailbox already exists!!'))
      } else {
        callback()
      }
    }
    return {
      countryList: country,
      form: {
        name: '',
        email: '',
        phone: '',
        pass: '',
        checkPass: '',
        genderSelect: 'Female',
        countrySelect: 'Anguilla',
        workOrstudy: '',
        sisuSelect: 'None',
        agree: false
      },
      rules: {
        name: [
          {
            required: true,
            message: 'Please enter a first name',
            trigger: 'blur'
          }
        ],
        email: [
          // { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          // { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] },
          { required: true, validator: validEmail, trigger: 'blur' }
        ],
        phone: [
          {
            required: true,
            message: 'Please enter your mobile phone number',
            trigger: 'blur'
          }
        ],
        pass: [{ required: true, validator: validatePass, trigger: 'blur' }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ],
        genderSelect: [
          { required: true, message: 'Please select a gender', trigger: 'blur' }
        ],
        countrySelect: [
          {
            required: true,
            message: 'Please select a country',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    // axios.post('/singup').then(res => {
    //   console.log('res', res)
    // }).catch(err => {
    //   console.log('erer', err)
    // })
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!this.form.agree) {
            // console.log('字 变红 ---', this.$refs.warm)
          } else {
            // console.log('form 提交,发送网络请求', this.form)
            const {
              name,
              email,
              phone,
              checkPass,
              genderSelect,
              countrySelect,
              workOrstudy,
              sisuSelect
            } = this.form || {}
            signup({
              name,
              email,
              phone,
              password: checkPass,
              gender: genderSelect,
              country: countrySelect,
              institute: workOrstudy,
              purpose: sisuSelect
            })
              .then((res) => {
                const { code, message } = res
                if (code == 202) {
                  this.$message.warning(`${message}`)
                } else if (code == 200) {
                  this.$message.success('Registration successful')
                  this.$router.push({ path: '/login' })
                } else if (code == 201) {
                  this.$message.warning(
                    'The email address is already registered'
                  )
                }
                // console.log('res', res)
              })
              .catch((err) => {
                console.log('err', err)
              })
          }
        } else {
          return false
        }
      })
    },
    handleURL() {
      window.open('term.html')
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  display: flex;
  /* justify-content: center; */
  align-items: center;
  flex-direction: column;
}

.title {
  display: inline-block;
  margin-top: 20px;
  padding-bottom: 3px;
  font-weight: 700;
  border-bottom: 3px solid #9c8bbf;
}

.form {
  margin-top: 18px;
}

/deep/.el-form-item__label {
  white-space: nowrap !important;
  margin-left: -90px;
}

.check {
  position: relative;
}

.check_article {
  position: absolute;
  top: 0px;
  left: 25px;
  white-space: nowrap;

  span {
    border-bottom: 1px solid #000;
    cursor: pointer;
    text-decoration: none;
  }
}

.warm {
  color: red;
}

.btn {
  background: #baafd3;
  color: #fff;
  width: 150px;
}

/deep/ .el-input__inner {
  width: 400px !important;
}
</style>
